<template>
  <div class="app-page">
    <el-table-form ref="form" :model="query">
      <el-form-item label="开户银行:" prop="bankName">
        <el-input v-model.trim="query.bankName" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="银行账号:" prop="cardNo">
        <el-input v-model.trim="query.cardNo" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="所属单位" prop="orgSNNo">
        <el-tree-select
          ref="treeSelect"
          v-model="orgSNNo"
          clearable
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择',
            loading: $loadingPlugin.getTreeData
          }"
          :tree-params=" {
            'expand-on-click-node': false,
            'highlight-current': true,
            'filterable':true,
            props:{
              value:'orgSNNo',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
          @search-clear="searchClear"
        />
      </el-form-item>
      <el-form-item label="是否启用:">
        <el-select v-model="query.accountStatus" filterable clearable placeholder="请选择">
          <el-option :value="1" label="启用" />
          <el-option :value="2" label="禁用" />
        </el-select>
      </el-form-item>
      <template v-slot:btns>
        <el-button v-permission="1" type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </template>
    </el-table-form>
    <div style="margin-bottom: 10px;" class="btn-group">
      <div>
        <el-button v-permission="2" type="primary" icon="el-icon-plus" @click="add">新增</el-button>
      </div>
    </div>
    <lb-table
      ref="lbTable"
      v-loading="$loadingPlugin.fetchData"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      highlight-current-row
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
    />
    <add ref="add" :tree-data="treeData" :org-list="orgList" />
  </div>
</template>

<script>
  import { queryBankAccountList } from '@/api'
  import getTreeData from '@/utils/jsonTree'
  import Add from './components/Add'
  import { mapGetters } from 'vuex'

  export default {
    name: 'bankAccount',
    components: {
      Add
    },
    data() {
      return {
        treeData: [],
        orgSNNo: '', // 所属机构orgSNNo
        query: {
          bankName: '',
          cardNo: '',
          accountStatus: ''
        },
        tableData: {
          total: 0,
          column: [
            {
              type: 'index'
            },
            {
              prop: 'bankName',
              label: '开户银行'
            },
            {
              prop: 'cardNo',
              label: '账号'
            },
            {
              prop: 'accountName',
              label: '账户名'
            },
            {
              prop: 'orgName',
              label: '使用机构'
            },
            {
              prop: 'accountStatus',
              label: '是否启用',
              render: (h, { row }) => {
                if (row.accountStatus === 1) {
                  return <span class='themeColor'>启用</span>
                } else if (row.accountStatus === 2) {
                  return <span>禁用</span>
                }
              }
            },
            {
              label: '操作',
              width: 50,
              fixed: 'right',
              render: (h, { row, $index }) => {
                return (
                  <div>
                    <el-link v-permission='3' size='mini' underline={false} onClick={() => this.update(row, $index)}>
                      编辑
                    </el-link>
                  </div>
                )
              }
            }
          ],
          data: []
        }
      }
    },
    computed: {
      ...mapGetters([
        'orgList'
      ])
    },
    async created() {
      await this.getTreeData()
    },
    methods: {
      searchClear() {
        if (this.$refs.treeSelect) this.$refs.treeSelect.keywords = ''
      },
      // 获取所属机构数据
      async getTreeData() {
        const data = this.orgList.map(item => ({
          label: item.orgName,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        if (this.orgSNNo) {
          const org = this.orgList.find(item => item.orgSNNo === this.orgSNNo)
          query.orgSNNo = this.orgSNNo
          query.orgId = org.orgId
        }
        const res = await queryBankAccountList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      async add() {
        await this.$refs.add.open()
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      async update(row) {
        await this.$refs.add.open(row)
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      }
    }
  }
</script>

<style lang="scss" scoped>
.el-form-item .el-form-item {
  margin-bottom: 0;
}
.merge-item {
  /deep/ .el-form-item__content {
    display: flex;
  }
}
.btn-group {
  display: flex;
  justify-content: space-between;
}
</style>
